package com.bfxy.rabbitmq.api.delay;

import java.util.HashMap;
import java.util.Map;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;

/**
 * <B>中文类名：</B>Consumer4Delay<BR>
 * <B>概要说明：</B><BR>
 * @author bhz（Alienware）
 * @since 2016年7月6日
 */
public class Consumer4Delay {  
  
    public static void main(String[] args) throws Exception {

        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("192.168.218.21");
            connectionFactory.setPort(5672);
            connectionFactory.setAutomaticRecoveryEnabled(true);

            connection = connectionFactory.newConnection();

            channel = connection.createChannel();

            // 延迟队列
            Map<String, Object> map = new HashMap<>();
            map.put("x-delayed-type", "topic");
            channel.exchangeDeclare("delay.exchange", "x-delayed-message", true, false, map);
            channel.queueDeclare("delay.queue", true, false, false, null);
            channel.queueBind("delay.queue", "delay.exchange", "delay.#");
            QueueingConsumer consumer = new QueueingConsumer(channel) ;
            channel.basicConsume("delay.queue", false, consumer) ;
            System.out.println("---------consume queue---------");
            while (true) {
                Delivery delivery = consumer.nextDelivery();
                String msg = new String(delivery.getBody());
                try {
                    System.out.println("received: " + msg);
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (channel != null) {
                channel.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
          
    }  
      
}  
